package com.liu.nb.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liu.nb.entity.SysMenu;
import com.liu.nb.entity.SysUser;
import com.liu.nb.mapper.SysMenuMapper;
import com.liu.nb.mapper.SysUserMapper;
import com.liu.nb.service.ISysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.nb.service.ISysUserService;
import com.liu.nb.util.dto.SysMenuDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.function.DoubleToIntFunction;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuziyi
 * @since 2021-08-05
 */
@Service
public class SysMenuService extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Override
    public List<SysMenuDto> getUserNav() {
        //从security上下文中获得用户名
        String username=(String)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //先获得sysUser
        SysUser sysUser = sysUserService.getByUsername(username);
        //再根据id获得user的权限id
        List<Long> menuIds = sysUserMapper.getNavMenuIds(sysUser.getId());
        //然后根据权限id获得权限实体
        List<SysMenu> menus = this.listByIds(menuIds);
        //然后把它转换成树状结构
        List<SysMenu> menuTree=buildTreeMenu(menus);
                //因为前后端数据模型不同意，所以再转换一边，把pojo转换成dto
        return convert(menuTree);
    }

    @Override
    public List<SysMenu> getMenuTree() {
        List<SysMenu> sysMenus = this.list(new QueryWrapper<SysMenu>().orderByAsc("orderNum"));
        return buildTreeMenu(sysMenus);
    }

    //pojo转换成dto
    private List<SysMenuDto> convert(List<SysMenu> menuTree) {
        List<SysMenuDto> menuDtos=new ArrayList<>();
        menuTree.forEach(m ->{
            SysMenuDto dto=new SysMenuDto();
            dto.setId(m.getId());
            dto.setName(m.getPerms());
            dto.setTitle(m.getName());
            dto.setComponent(m.getComponent());
            dto.setIcon(m.getIcon());
            dto.setPath(m.getPath());
            //如果有children
            if (m.getChildren().size() > 0) {
                //子节点调用当前方法，（递归）
                dto.setChildren(convert(m.getChildren()));
            }
            menuDtos.add(dto);
        });
        return menuDtos;
    }
//  把实体转换成树状结构
    private List<SysMenu> buildTreeMenu(List<SysMenu> menus) {
        List<SysMenu> finalMenus=new ArrayList<>();
        //先找到每个menu的children,循环判断id 和 parentID
        for (SysMenu menu : menus) {
            for (SysMenu e : menus) {
//              如果id 和 parentID相等，父节点的childrenList就add
                if (menu.getId()==e.getParentId()){
                    menu.getChildren().add(e);
                }
            }
//              如果节点的id=0，那么他就是顶级节点
            if (menu.getParentId()==0){
                finalMenus.add(menu);
            }
        }
        return finalMenus;
    }
}
